#include  "include.h"
#include "spi.h"
#include "i2c.h"
#include "gpio.h"
#include "uart.h"
#include "adc.h"
#include "sfr.h"
#include "timer.h"
#include "bluetooth.h"
#include "pwm.h"


void pwm_test(void);
void I2C_test(void);
void Uart_init(void);
void spi_test(void);
void uart_test(void);

void Timer0Interrupt(void);
void timer_init(void);

extern void I2CInit(void);
extern void SYS_Init(void);
extern void APP_Process(void);
extern void TMR_Process(void);
extern void SYS_EnterLpm(void);
extern void BT_BleRoutine(void);




void InitTimer0(void)
{
    TMOD = 0x01;
    TH0 = 0x0FB;
    TL0 = 0x50;
    EA = 1;
    ET0 = 1;
    TR0 = 1;
}

void gpio_init()
{
    //input keys init
    GPIO_Init(GPIO_Pin_16_23_Group,GPIO_Pin_23,GPIO_Mode_IN,GPIO_PuPd_UP);
    GPIO_Init(GPIO_Pin_24_31_Group,GPIO_Pin_24,GPIO_Mode_IN,GPIO_PuPd_UP);
    GPIO_Init(GPIO_Pin_24_31_Group,GPIO_Pin_25,GPIO_Mode_IN,GPIO_PuPd_UP);
    GPIO_Init(GPIO_Pin_24_31_Group,GPIO_Pin_26,GPIO_Mode_IN,GPIO_PuPd_UP);

    //led init
    GPIO_Init(GPIO_Pin_24_31_Group,GPIO_Pin_27,GPIO_Mode_OUT,GPIO_PuPd_UP);

    // led set
    GPIO_WriteBit(GPIO_Pin_24_31_Group, GPIO_Pin_27,LOW);

    //GPIO_SetWakeup(GPIO_Pin_24_31_Group,GPIO_Pin_24,ENABLE,LOW);
    //GPIO_SetWakeup(GPIO_Pin_24_31_Group,GPIO_Pin_25,ENABLE,LOW);
    //GPIO_SetWakeup(GPIO_Pin_24_31_Group,GPIO_Pin_26,ENABLE,LOW);
    GPIO_SetWakeup(GPIO_Pin_16_23_Group,GPIO_Pin_23,ENABLE,LOW);

    //init 24bit ADC TM7711 SCK PIN
    GPIO_Init(GPIO_Pin_24_31_Group, GPIO_Pin_28, GPIO_Mode_OUT, GPIO_PuPd_UP);

    //init 24bit ADC TM7711 DOUT PIN
    GPIO_Init(GPIO_Pin_0_7_Group, GPIO_Pin_4, GPIO_Mode_IN, GPIO_PuPd_DOWN);

    //moter en pin config
    GPIO_Init(GPIO_Pin_8_15_Group, GPIO_Pin_8, GPIO_Mode_OUT, GPIO_PuPd_UP);
    GPIO_WriteBit(GPIO_Pin_8_15_Group, GPIO_Pin_8,LOW);//enable while high


	//

    //GPIO_Init(GPIO_Pin_0_7_Group, GPIO_Pin_3, GPIO_Mode_OUT, GPIO_PuPd_DOWN);
}
void mpu9250_init(void)
{
//    I2C_InitDef init_struct;
//  init_struct.I2C_ClockSpeed = IICD_CLOCKSPEED400KHZ;
//  I2C_init(&init_struct);

    u8_t gyro_scale = 0x18;
    I2C_WriteDataFromBuff(MPU9250_ADDRESS, 27,1, &gyro_scale);
    // Configure accelerometers range
    u8_t acc_scale = 0x18;
    I2C_WriteDataFromBuff(MPU9250_ADDRESS, 28,1, &acc_scale);
    // Set by pass mode for the magnetometers
    u8_t magnet_meter = 0x02;
    I2C_WriteDataFromBuff(MPU9250_ADDRESS, 0x37,1, &magnet_meter);

    // Request first magnetometer single measurement
    u8_t fst_measure = 0x16;
    I2C_WriteDataFromBuff(MAG_ADDRESS, 0x0A,1, &fst_measure);
}

void main(void)
{
    SYS_Init();
    gpio_init();
    I2CInit();
    mpu9250_init();
    Uart_init();
    while(1)
    {
        TMR_Process();
        BT_BleRoutine();
        APP_Process();
        //SYS_EnterLpm();
    }
}

void Timer0Interrupt(void)// interrupt 1
{
    TH0 = 0x0FB;
    TL0 = 0x50;

}
void I2CInit()
{
    I2C_InitDef init_struct;
    init_struct.I2C_ClockSpeed = IICD_CLOCKSPEED400KHZ;
    I2C_init(&init_struct);
}

void I2C_test()
{
    u8_t Data = 5;
    u8_t send_buff[5] = {Data++,Data++,Data++,Data++,Data++};
    u8_t DevAddr = DEV_ADDR_EEPROM;
    u32_t RegAddr = I2C_REGADDR16BITS(REG_ADDR_EEPROM);
    I2CInit();
    I2C_WriteDataFromBuff(DevAddr,RegAddr,5,send_buff);
}

void spi_test()
{
    u16_t RxLen = 0;
    u16_t TxLen = 10;
    u8_t RxBuff[10];
    u8_t  TxBuff[10] = {0,1,2,3,4,5,6,7,8,9};
    YC_SpiControl ( RxLen,  TxLen,  RxBuff,  TxBuff) ;
    while(1);
}
void uart_test()
{
    u8_t UartRx[50];
    u16_t Len;
    while(1)
    {
        Len = REG16(ADDR_REG_UART_RXITEMS);
        if(Len < 50)
        {
            UART_ReciveData(UartRx, Len);
            delayms(10);
            UART_SendData(UartRx, Len);

        }
        else
        {
            return;
        }
    }
}

void Uart_init()
{
    UartInitRegDef DbgUart =
    {
        .USART_BaudRate = UART_BAUD_115200,
        .USART_WordLength = USART_WordLength_8b,
        .USART_StopBits = USART_StopBits_1,
        .USART_Parity = USART_Parity_Even,
        .USART_Mode = USART_Mode_duplex,
        .USART_HardwareFlowControl = USART_HardwareFlowControl_None,
        .USART_TXaddr = UART_RSADDR,
        .USART_RXaddr = UART_TSADDR,
        .USART_TXLen = UART_TxLen,
        .USART_RXLen = UART_RxLen,
    };

    UART_Init(&DbgUart);
}
